package com.jecy.springcloud.service;

import com.jecy.springcloud.model.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by huijun on 17/6/30.
 */
@Service
public class UserService {
    @Autowired
    private RestTemplate restTemplate;
    private String SERVICE_NAME="cloud-simple-service";
    //@HystrixCommand(fallbackMethod = "fallbackSearchAll")
    public List<User> readUserInfo() {
        List<User> users = restTemplate.getForObject("http://"+SERVICE_NAME+"/user",List.class);
        return users;
    }

    private List<User> fallbackSearchAll() {
        System.out.println("HystrixCommand fallbackMethod handle!");
        List<User> ls = new ArrayList<User>();
        User user = new User();
        user.setUsername("TestHystrixCommand");
        ls.add(user);
        return ls;
    }
}
